// pages/my/my.js
const db = wx.cloud.database();
const _ = db.command
const app = getApp();
const plugin = requirePlugin("WechatSI")
const manager = plugin.getRecordRecognitionManager()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_login:false,
    user:null,
    goods_num_id:"",
   
  },

  onLoad: function () {
    this.initRecord()
  },
  //去出售商品页面
  sell:function(){
    wx.navigateTo({
      url: '../sell/sell'
    })
  },
  //去管理页面
  control:function(){
    wx.navigateTo({
      url: '../control_sell/control_sell'
    })
  },
  //初始化
  onLoad: function (options) {
    let that=this;
    // let b={"bbb":122};
    // Object.assign(b,{"aaa":122});
  //  console.log(b);
    wx.getStorage({
      key: 'goods_num_id',
      success(res) {
        that.setData({
          goods_num_id:res.data
        })
      }
    })
    wx.getStorage({
      key: 'openid',
      success(res) {
          app.globalData.openid=res.data
      }
    })

    wx.getStorage({
      key: 'user',
      success(res) {
        that.setData({
          user: JSON.parse(res.data),
          is_login:true
        })
        app.globalData.user = JSON.parse(res.data);
        app.globalData.is_login = true
        console.log(res.data);
      }
    })

  },
  //获取用户登录信息
  bindgetuserinfo:function(e){
    let that=this;
    wx.cloud.callFunction({
      name: 'test',
      complete: res => {
        console.log('callFunction test result: ', res)
        app.globalData.openid = res.result.openid;
        console.log(app.globalData.openid)
        wx.setStorage({
          key: "openid",
          data: res.result.openid
        })
      }
    })
  //  console.log(e)
    this.setData({
      user:e.detail.userInfo,
      is_login:true
    })
    wx.setStorage({
      key: "user",
      data: JSON.stringify(e.detail.userInfo)
    })
    //设置到全局变量里面去
    this.setUser();
 //创建购物车用户信息： 购买历史，当前购物车的商品数目 ,  goods_num_id是标识符
    db.collection('goods_num').doc(that.data.goods_num_id).get({
      success: function (res) {
        console.log(res);
        app.globalData.goods_num_id = res.data._id;
      },
      fail: function (err) {
        db.collection('goods_num').add({
          data: {
            bought_histroy: [],
            goods_number: 0
          },
          success: function (e) {
            console.log(e);
            app.globalData.goods_num_id = e._id;
            wx.setStorage({
              key: "goods_num_id",
              data: e._id
            })
          }
        })
      }
    })
  },
  setUser:function(){
    app.globalData.is_login=this.data.is_login;
    app.globalData.user=this.data.user;
  //  console.log(app.globalData.is_login)
  },
  
  //订单列表数据库里面应该存储的信息，购买者的openid,发布者的openid,购买的物品的数目，购买的物品的id
  order_list:function(){
    wx.navigateTo({
      url: '../order_list/order_list',  
    })
  },
  //去销售情况页面
  to_sell_info:function(){
    wx.navigateTo({
      url: '../sell_info/sell_info',
    })
  },
  //去反馈页面
  to_feedback:function(){
    wx.navigateTo({
      url: '../feedback/feedback',
    })
  }
})